/**
 * Computer Network, Programming Assignment #2
 * 
 * vSwitch Class
 * 
 * Stands for each switch, start/connect to peers/echo on/echo off
 * 
 * @author Yancheng Li
 *
 */

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;


public class vSwitch {
	public static void main(String[] args) {			
		
		if(args.length != 2){
			System.out.println("Usage: java vSwitch x y");
			return;
		}
			
		PGPeer peer = new PGPeer(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
		
		peer.startServer();
		
		
	
		DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
		String line = "";
		help();
		while(line.compareTo("exit") != 0){
			try {
				System.out.print("\n>>");
				
				line = in.readLine();
				
				String [] list = line.split(" ");
				
				if(list.length > 4){
					System.out.println("Error command");					
				}
				
				//connect to peers
				
				if(list[0].compareTo("connect") == 0){
					peer.connectPeers();					
					System.out.print("\n>>");
					continue;
				}
				
				if (list[0].compareTo("update") == 0){					
					peer.startGRIP();
					System.out.print("\n>>");
					continue;
				}
				
				// Change a particular node
				if (list[0].compareTo("change") == 0){
					try{
						Point p = new Point(Integer.parseInt(list[1]), Integer.parseInt(list[2]));
						if (peer.checkGRIPTable(p)){
							peer.setGRIPTable(p, Integer.parseInt(list[3]));
						}
					}catch (Exception e){
						System.out.println("Could not change table because " + e.getMessage());
					}			
					continue;
				}
				
				if (list[0].compareTo("ping") == 0){
					Socket sendSocket = new Socket("localhost", Integer.parseInt(list[1]));
					
				}
					
				
				//send test packet to grid(1, 2) port 8000
				if(list[0].compareTo("test") == 0){
					PSPUtil.print("Test to send a packet to grid (1, 2) port 8000", true);
					GPDatagram packet = new GPDatagram(1, 2, 8000, "test packet".toCharArray());
					peer.sendPacket(packet);
					continue;
				}
				
				//echo on
				if(list[0].compareTo("echoon") == 0){
					peer.echoOn();
					continue;
				}
				
				//echo off
				if(list[0].compareTo("echooff") == 0){
					peer.echoOff();
					continue;
				}
				
				//help info
				if(list[0].compareTo("help") == 0){
					help();
					continue;
				}
				System.out.println("Error command");	
				
				
			} catch (IOException e) {

				e.printStackTrace();
			}

		}
		peer.shutdown();
		System.exit(0);
	}
	public static void help(){
		System.out.println("\n======================================");
		System.out.println("Command List:");
		System.out.println("[1]connect:\n connect to all neighbor peers");
		System.out.println("[2]echoon:\n turn on echo mode");
		System.out.println("[3]echooff:\n turn off echo mode");
		System.out.println("[4]update:\n starts Link table update");
		System.out.println("[5]change x, y d:\n change the Link table x,y from this switch with distance d ");
		System.out.println("help: print help info");
		System.out.println("======================================");
		System.out.print("\n>>");
	}
}

